package com.xtaller.sdboot.util;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.sdboot.utils.helper.JsonHelper;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : Taller
 * @date : 2020/3/4 09:08
 * @Description :
 */
@Slf4j
@Component
public class RedisUtil {
    @Autowired private StringRedisTemplate stringRedisTemplate;
    @Autowired private RedisTemplate redisTemplate;

    /**
     * 删除键值对
     * @param key
     * @return
     */
    public void delete(String key){
        stringRedisTemplate.delete(key);
    }

    /**
     * 放键值对
     * 永久
     * @param key
     * @param value
     */
    public Boolean set(String key, String value){
        return set(key, value, -1L);
    }
    public Boolean set(String key, Object value){
        return set(key, value, -1L);
    }
    public Boolean set(String key, Object value, long second){
        try {
            if(value.getClass().isPrimitive()){
                if(second < 0L){
                    stringRedisTemplate.opsForValue().set(key, value.toString());
                }else{
                    stringRedisTemplate.opsForValue().set(key, value.toString(), second, TimeUnit.SECONDS);
                }
            }else {
                if(second < 0L) {
                    stringRedisTemplate.opsForValue().set(key, JsonHelper.o2s(value));
                }else {
                    stringRedisTemplate.opsForValue().set(key, JsonHelper.o2s(value), second, TimeUnit.SECONDS);
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据key取value
     *
     * @param key
     * @return
     */
    public String get(String key){
        String val = null;
        try {
            val = stringRedisTemplate.opsForValue().get(key);
            val = val == null ? null : val;
        } catch (Exception ex) {
        }
        return val;
    }
    public JSONObject getJsonObject(String key){
        var str = get(key);
        if(str != null){
            return JsonHelper.s2j(str);
        }
        return null;
    }
    public <T> T get(String key, Class<T> cls){
        var str = get(key);
        if(str != null){
            return JsonHelper.s2m(str, cls);
        }
        return null;
    }
    public <T> List<T> getList(String key, Class<T> cls){
        var str = get(key);
        if(str != null){
            return JsonHelper.parseArray(str, cls);
        }
        return null;
    }
    public List<JSONObject> getListJsonObject(String key){
        var str = get(key);
        if(str != null){
            return JsonHelper.parseArray(str, JSONObject.class);
        }
        return null;
    }
}
